#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <string>
#include "iotc_app_proccess_access_token.h"
// permission
#include "accesstoken_kit.h"
#include "nativetoken_kit.h"
#include "token_setproc.h"

#define IOTC_APP_PROCESS_NAME "yarward_iotc_ap"
#define IOTC_APP_PERMISSION_WIFI_OPT "ohos.permission.SET_WIFI_INFO"
using namespace OHOS::Security::AccessToken;

#if 0
int SetIotcAppProcessPermission()
{
    int ret = 0;
    uint32_t unmaskedFlag = PermissionFlag::PERMISSION_ALLOW_THIS_TIME;
    AccessTokenID tokenId = AccessTokenKit::GetNativeTokenId(IOTC_APP_PROCESS_NAME);
    if (tokenId == INVALID_TOKENID) {
        printf("GetNativeTokenId failed, tokenId:%d\n", tokenId);
        return -1;
    }

    ret = AccessTokenKit::GrantPermission(tokenId, IOTC_APP_PERMISSION_WIFI_OPT, unmaskedFlag);

    if (ret != 0) {
        printf("SetProcessPermission failed, ret:%d\n", ret);
    } else {
        printf("SetProcessPermission success.\n");
    }
    return ret;
}

#else
int SetIotcAppProcessPermission()
{
    uint64_t tokenId;
    const char *perms[] = {
        "ohos.permission.ACCESS_BLUETOOTH",
        "ohos.permission.USE_BLUETOOTH",
        "ohos.permission.MANAGE_BLUETOOTH",
        "ohos.permission.DISCOVER_BLUETOOTH",
        "ohos.permission.GET_WIFI_INFO",
        "ohos.permission.GET_NETWORK_INFO",
        "ohos.permission.GET_WIFI_INFO_INTERNAL",
        "ohos.permission.SET_WIFI_INFO",
        "ohos.permission.SET_NETWORK_INFO",
        "ohos.permission.GET_WIFI_LOCAL_MAC",
        "ohos.permission.SET_WIFI_CONFIG",
        "ohos.permission.GET_WIFI_CONFIG",
        "ohos.permission.MANAGE_WIFI_HOTSPOT",
        "ohos.permission.MANAGE_WIFI_CONNECTION",
        "ohos.permission.GET_WIFI_PEERS_MAC",
        "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED",
        "ohos.permission.FILE_ACCESS_MANAGER",
        "ohos.permission.STORAGE_MANAGER"
    };
    int size = sizeof(perms) / sizeof(perms[0]);
    NativeTokenInfoParams infoInstance = {
        .dcapsNum = 0,
        .permsNum = size,
        .aclsNum = 0,
        .dcaps = NULL,
        .perms = perms,
        .acls = NULL,
        .processName = "com.iotc.bin",
        .aplStr = "normal",
    };
    tokenId = GetAccessTokenId(&infoInstance);
    if (tokenId == INVALID_TOKENID) {
        printf("GetAccessTokenId failed, tokenId:%llu\n", tokenId);
        return -1;
    }
    SetSelfTokenID(tokenId);
    OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
    return 0;
}
#endif
